package com.uberspot.yangchenglun

import android.content.SharedPreferences
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity

class HistoryActivity : AppCompatActivity() {
    private lateinit var listViewHistory: ListView
    private lateinit var historyAdapter: ArrayAdapter<String>
    private lateinit var buttonBack: Button
    private lateinit var sharedPreferences: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_history)

        listViewHistory = findViewById(R.id.listViewHistory)
        buttonBack = findViewById(R.id.buttonBack)
        sharedPreferences = getSharedPreferences("AppPreferences", MODE_PRIVATE)

        val historyList = loadHistory()
        historyAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, historyList.toList())
        listViewHistory.adapter = historyAdapter

        buttonBack.setOnClickListener {
            finish() // This will take the user back to the previous activity
        }
    }

    private fun loadHistory(): Set<String> {
        return sharedPreferences.getStringSet("history", null) ?: setOf()
    }

    private fun saveHistory(historyList: Set<String>) {
        val editor = sharedPreferences.edit()
        editor.putStringSet("history", historyList)
        editor.apply()
    }
}